package com.ikkong.biz.controller;

import java.util.Date;
import java.util.Map;

import org.beetl.sql.ext.jfinal.Trans;

import com.ikkong.core.base.BaseController;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.qy.util.WeixinApi;
import com.ikkong.system.controller.base.AdminBaseController;
import com.ikkong.biz.model.FlowSegment;
import com.ikkong.biz.service.FlowSegmentService;
import com.ikkong.biz.service.impl.FlowSegmentServiceImpl;
import com.jfinal.aop.Before;

/**
 * Generated by Blade.
 * 2016-11-24 10:55:05
 */
public class FlowSegmentController extends AdminBaseController {
	private static String CODE = "flowSegment";
	private static String PERFIX = "tb_flow_segment";
	private static String LIST_SOURCE = "FlowSegment.list";
	private static String BASE_PATH = "/biz/flowSegment/";
	
	FlowSegmentService service = new FlowSegmentServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "flowSegment.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "flowSegment_add.html");
	}

	public void edit() {
		String id = getPara(0);
		FlowSegment flowSegment = service.findById(id);
		setAttr("model", JsonKit.toJson(flowSegment));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "flowSegment_edit.html");
	}

	public void view() {
		String id = getPara(0);
		FlowSegment flowSegment = service.findById(id);
		setAttr("model", JsonKit.toJson(flowSegment));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "flowSegment_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	@Before(Trans.class)
	public void save() {
		FlowSegment flowSegment = mapping(PERFIX, FlowSegment.class);
		flowSegment.setCtime(new Date());
		flowSegment.setStat(0);
		
		boolean temp = service.next(flowSegment);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		FlowSegment flowSegment = mapping(PERFIX, FlowSegment.class);
		boolean temp = service.update(flowSegment);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
